<template>
  <!-- 新增部门的弹层 -->
  <el-dialog :title="title" :visible="dialogVisible" @close="closeDialog">
    <!-- 表单组件  el-form   label-width设置label的宽度   -->
    <!-- 匿名插槽 -->
    <el-form
      ref="addRef"
      v-loading="loading"
      label-width="120px"
      :model="addForm"
      :rules="rules"
    >
      <el-form-item label="部门名称" prop="name">
        <el-input
          v-model="addForm.name"
          style="width: 80%"
          placeholder="1-50个字符"
        />
      </el-form-item>
      <el-form-item label="部门编码" prop="code">
        <el-input
          v-model="addForm.code"
          style="width: 80%"
          placeholder="1-50个字符"
        />
      </el-form-item>
      <el-form-item label="部门负责人" prop="manager">
        <el-select
          v-model="addForm.manager"
          style="width: 80%"
          placeholder="请选择"
        ><el-option
          v-for="item in managerList"
          :key="item.id"
          :label="item.username"
          :value="item.username"
        /></el-select>
      </el-form-item>
      <el-form-item label="部门介绍" prop="introduce">
        <el-input
          v-model="addForm.introduce"
          style="width: 80%"
          placeholder="1-300个字符"
          type="textarea"
          :rows="3"
        />
      </el-form-item>
    </el-form>
    <!-- el-dialog有专门放置底部操作栏的 插槽  具名插槽 -->
    <div slot="footer">
      <el-button
        type="primary"
        size="small"
        @click="addDepartment"
      >确定</el-button>
      <el-button size="small" @click="closeDialog">取消</el-button>
    </div>
  </el-dialog>
</template>
<script>
import {
  reqAddDepartment,
  reqEditDepartDetailById,
  reqGetDepartDetailById
} from '@/api/department'
import { reqGetEmployeesSimple } from '@/api/employees'
export default {
  props: {
    dialogVisible: {
      type: Boolean,
      default: false
    },
    nodeData: {
      type: Object,
      required: true
    },
    // 原始数据
    sourceDepts: {
      type: Array,
      required: true
    }
  },
  data() {
    // 同级部门名称不能重名自定义校验规则
    const checkNameRepeat = (rule, value, cb) => {
      if (this.addForm.id && value === this.nodeData.name) return cb()
      if (this.addForm.id) {
        // 编辑部门 查找的匹配是兄弟部门
        const arr = this.sourceDepts.filter(item => item.pid === this.addForm.pid)
        const boo = arr.some(item => item.name === value)
        boo ? cb(new Error(`已经存在${value}部门了`)) : cb()
      } else {
        // 新增部门 找的是子部门
        const boo = this.nodeData.children.some((item) => item.name === value)
        boo ? cb(new Error(`已经存在${value}部门了`)) : cb()
      }
    }
    // 部门编码的自定义校验规 (新增部门编码不允许和任意部门编码重名)
    const checkCodeRepeat = (rule, value, cb) => {
      if (this.addForm.id && value === this.nodeData.code) return cb()
      const boo = this.sourceDepts.some((item) => item.code === value)
      boo ? cb(new Error(`已经存在${value}编码了`)) : cb()
    }
    return {
      addForm: {
        name: '',
        code: '',
        manager: '',
        introduce: ''
      },
      managerList: [],
      rules: {
        name: [
          {
            required: true,
            message: '部门名称不能为空',
            trigger: ['changer', 'blur']
          },
          {
            min: 1,
            max: 50,
            message: '部门名称必须在1-50个字符之间',
            trigger: ['changer', 'blur']
          },
          { validator: checkNameRepeat, trigger: ['changer', 'blur'] }
        ],
        code: [
          {
            required: true,
            message: '部门编码不能为空',
            trigger: ['changer', 'blur']
          },
          {
            min: 1,
            max: 50,
            message: '部门编码必须在1-50个字符之间',
            trigger: ['changer', 'blur']
          },
          { validator: checkCodeRepeat, trigger: ['changer', 'blur'] }
        ],
        manager: [
          {
            required: true,
            message: '请选择部门负责人',
            trigger: ['changer', 'blur']
          }
        ],
        introduce: [
          {
            required: true,
            message: '部门介绍不能为空',
            trigger: ['change', 'blur']
          },
          {
            min: 1,
            max: 300,
            message: '部门介绍必须在1-300个字符之间',
            trigger: ['change', 'blur']
          }
        ]
      },
      title: '添加部门',
      loading: false
    }
  },
  created() {
    this.getEmployeesSimple()
  },
  methods: {
    closeDialog() {
      this.$refs.addRef.resetFields()
      this.addForm = {
        name: '',
        code: '',
        manager: '',
        introduce: ''
      }
      this.$emit('update:dialogVisible', false)
    },
    // 新增部门
    addDepartment() {
      // console.log(this.nodeData.id) // pid: 部门的id 当做新增的部门的id
      this.$refs.addRef.validate(async(boo) => {
        if (!boo) return
        if (this.addForm.id) {
          await reqEditDepartDetailById(this.addForm)
        } else {
          await reqAddDepartment({ ...this.addForm, pid: this.nodeData.id })
        }
        this.closeDialog()
        this.$emit('getDepts')
      })
    },
    // 获取员工简单列表
    async getEmployeesSimple() {
      const { data } = await reqGetEmployeesSimple()
      this.managerList = data
    },
    // 根据id获取部门详情
    async getDepartDetailById(id) {
      // 发送请求前开启loading
      this.loading = true
      const { data } = await reqGetDepartDetailById(id)
      this.addForm = data
      // 请求结果回来关闭loading
      this.loading = false
    }
  }
}
</script>
